<?php
class People
{
	const DBTable = "tbl_people";

	public $People_id;	public $People_md5;	public $Title;	public $Role;	public $Strapline;	public $Description;
	public $Twitter_username;	public $Image;
	public $Image_thumbnail;	public $Meta_title;	public $Meta_description;	public $Meta_keywords;	public $Status;	public $Ordering;	public $Is_deleted;	public $Createdate;	public $Updatedate;
	private $__mysql;

	public function __construct()
	{
		$this->People_id = 0;		$this->People_md5 = null;		$this->Title = null;		$this->Role = null;		$this->Strapline = null;		$this->Description = null;
		$this->Twitter_username = null;		$this->Image_thumbnail = null;
		$this->Image = null;		$this->Meta_title = null;		$this->Meta_description = null;		$this->Meta_keywords = null;		$this->Status = 2;		$this->Ordering = 0;		$this->Is_deleted = 0;		$this->Createdate = date("Y-m-d H:i:s");		$this->Updatedate = date("Y-m-d H:i:s");
		$this->__mysql = new MYSQL;
	}

	public function Save()
	{
		$sqlStr = "insert into ".self::DBTable."
				(title, role, strapline, description, twitter_username, image, image_thumbnail, status, createdate, updatedate, meta_title)
				values
				(
				'".mysql_real_escape_string($this->Title)."', 				'".mysql_real_escape_string($this->Role)."', 				'".mysql_real_escape_string($this->Strapline)."', 				'".mysql_real_escape_string($this->Description)."', 
				'".mysql_real_escape_string($this->Twitter_username)."', 				'".mysql_real_escape_string($this->Image)."', 
				'".mysql_real_escape_string($this->Image_thumbnail)."',				'".$this->Status."', 				'".$this->Createdate."', 				'".$this->Updatedate."', 
				'".mysql_real_escape_string($this->Title)."'
				)";
			$this->__mysql->Exec_query($sqlStr);
			$this->People_id = $this->__mysql->Get_insert_id();

			//create an md5
			$this->People_md5 = substr(md5($this->People_id), 0, 10);
			$this->Quick_update("people_md5", $this->People_md5);

			return $this->People_id;
	}

	public function Update()
	{
		//deal with any assets
		$query = "select image, image_thumbnail from ".self::DBTable." where people_id = '".mysql_real_escape_string($this->People_id)."'";
		$recordset = $this->__mysql->Exec_query($query);
		$result = $this->__mysql->Get_data_assoc($recordset);

		if($this->Image != "" || $this->Image == "delete")
		{
			if($this->Image == "delete")
			{
				$this->Image = null;
				@unlink(ASSET_STORE_PEOPLE . $result['image']);
			}

			$this->Quick_update("image", $this->Image);
		}

		if($this->Image_thumbnail != "" || $this->Image_thumbnail == "delete")
		{
			if($this->Image_thumbnail == "delete")
			{
				$this->Image_thumbnail = null;
				@unlink(ASSET_STORE_PEOPLE . $result['image_thumbnail']);
			}

			$this->Quick_update("image_thumbnail", $this->Image_thumbnail);
		}

		//
	
		$sqlStr = "update ".self::DBTable." set
			title = '".mysql_real_escape_string($this->Title)."', 			role = '".mysql_real_escape_string($this->Role)."', 			strapline = '".mysql_real_escape_string($this->Strapline)."', 			description = '".mysql_real_escape_string($this->Description)."', 
			twitter_username = '".mysql_real_escape_string($this->Twitter_username)."', 			image = '".mysql_real_escape_string($this->Image)."', 
			image_thumbnail = '".mysql_real_escape_string($this->Image_thumbnail)."', 			status = '".$this->Status."', 			updatedate = '".$this->Updatedate."'
			where people_id = '".mysql_real_escape_string($this->People_id)."'";
			$this->__mysql->Exec_query($sqlStr);
	}

	public function Quick_update($column = null, $value = null)
	{
		$sqlStr = "update ".self::DBTable." set
			".$column." = '".mysql_real_escape_string($value)."',
			 updatedate = '".$this->Updatedate."' 
			where people_id = '".mysql_real_escape_string($this->People_id)."'";
			$this->__mysql->Exec_query($sqlStr);
	}

	public function Update_ordering($id_array = null, $people_id = 0)
	{
		if(count($id_array) > 0)
		{
			$i = 1;
			foreach($id_array as $id)
			{
				//update the ordering of the banner that is about to be replaced
				$sqlStr = "update ".self::DBTable." 
					set ordering = ".$i." 
					where 
					people_id = '".$id."'";
				//echo $sqlStr . "<br />";
				$this->__mysql->Exec_query($sqlStr);
				$i++;
			}
		}
	}

	public function List_all($limit = null, $offset = null)
	{
		$sqlStr = "select *
			from ".self::DBTable."
			where is_deleted = 0 
			order by ordering asc";
		if(isset($limit) && isset($offset)){
			$sqlStr .= " limit $offset, $limit";
		}
		//echo $sqlStr;
		$recordset = $this->__mysql->Exec_query($sqlStr);
		return $data_array = $this->__mysql->Get_data_array($recordset);
	}

	public function List_frontend_people($limit = null, $offset = null)
	{
		$sqlStr = "select *
			from ".self::DBTable."
			where is_deleted = 0 and
			status = 1
			order by ordering asc";
		if(isset($limit) && isset($offset)){
			$sqlStr .= " limit $offset, $limit";
		}
		//echo $sqlStr;
		$recordset = $this->__mysql->Exec_query($sqlStr);
		return $data_array = $this->__mysql->Get_data_array($recordset);
	}

	public function List_all_selectlist()
	{
		$sqlStr = "select people_id, title
			from ".self::DBTable."
			where is_deleted = 0 
			order by title";
		//echo $sqlStr;
		$recordset = $this->__mysql->Exec_query($sqlStr);
		return $data_array = $this->__mysql->Get_data_array($recordset);
	}

	public function Delete($people_id = 0)
	{
		$sqlStr = "update ".self::DBTable." set is_deleted = 1 where people_id = '".mysql_real_escape_string($people_id)."'";
			$this->__mysql->Exec_query($sqlStr);

		$sqlStr = "optimize table ".self::DBTable."";
			$this->__mysql->Exec_query($sqlStr);
	}

	public function Select($people_id = 0)
	{
		$sqlStr = "select * from ".self::DBTable." where people_id = '".mysql_real_escape_string($people_id)."'";
		$recordset = $this->__mysql->Exec_query($sqlStr);

		if($this->__mysql->Get_num_rows($recordset) > 0)
		{
			$result = $this->__mysql->Get_data_assoc($recordset);
				$this->People_id = $result['people_id'];				$this->People_md5 = $result['people_md5'];				$this->Title = $result['title'];				$this->Role = $result['role'];				$this->Strapline = $result['strapline'];				$this->Description = $result['description'];
				$this->Twitter_username = $result['twitter_username'];				$this->Image = $result['image'];
				$this->Image_thumbnail = $result['image_thumbnail'];				$this->Meta_title = $result['meta_title'];				$this->Meta_description = $result['meta_description'];				$this->Meta_keywords = $result['meta_keywords'];				$this->Status = $result['status'];				$this->Ordering = $result['ordering'];				$this->Is_deleted = $result['is_deleted'];				$this->Createdate = $result['createdate'];				$this->Updatedate = $result['updatedate'];
			return true;
		}
		else
		{
			return false;
		}
	}

	public function Select_by($fieldname = null, $value = null)
	{
		if(!is_null($fieldname))
		{
			$sqlStr = "select * from ".self::DBTable." where ".$fieldname." = '".mysql_real_escape_string($value)."'";
			$recordset = $this->__mysql->Exec_query($sqlStr);

			if($this->__mysql->Get_num_rows($recordset) > 0)
			{
				$result = $this->__mysql->Get_data_assoc($recordset);
				$this->People_id = $result['people_id'];				$this->People_md5 = $result['people_md5'];				$this->Title = $result['title'];				$this->Role = $result['role'];				$this->Strapline = $result['strapline'];				$this->Description = $result['description'];
				$this->Twitter_username = $result['twitter_username'];				$this->Image = $result['image'];
				$this->Image_thumbnail = $result['image_thumbnail'];				$this->Meta_title = $result['meta_title'];				$this->Meta_description = $result['meta_description'];				$this->Meta_keywords = $result['meta_keywords'];				$this->Status = $result['status'];				$this->Ordering = $result['ordering'];				$this->Is_deleted = $result['is_deleted'];				$this->Createdate = $result['createdate'];				$this->Updatedate = $result['updatedate'];
				return true;
			}
			else
			{
				return false;
			}
		}
	}
}
?>